<?php
/**
 * $Id: winners.php 7 2010-04-26 20:05:00Z webbochsant $
 *  @package	Joomla
 *  @author Daniel Eliasson
* 	@copyright	Copyright (C) 2010 Stilero. All rights reserved.
* 	@license	GNU/GPL, see LICENSE.php
* 	Joomla! is free software. This version may have been modified pursuant
* 	to the GNU General Public License, and as distributed it includes or
* 	is derivative of works licensed under the GNU General Public License or
* 	other free or open source software licenses.
*/

// no direct access
defined('_JEXEC') or die('Restricted access'); 

jimport( 'joomla.application.component.model' );
 

/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class VmcompetitionsModelWinners extends JModel
{
    /**
     * Hellos data array
     *
     * @var array
     */
    var $_data;
    
	    /**
	   * Items total
	   * @var integer
	   */
	  var $_total = null;
	 
	  /**
	   * Pagination object
	   * @var object
	   */
  var $_pagination = null;
    
  function __construct()
  {
        parent::__construct();
 
        global $mainframe, $option;
 
        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
 
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
  }
  
 
    /**
     * Returns the query
     * @return string The query to be used to retrieve the rows from the database
     */
    function _buildQuery()
    {
        $query = ' SELECT * '
            . ' FROM #__vm_competition_winners'
        ;
        
        $query = 'SELECT w . * , cmp.title AS comp_name, cnt.name AS cont_name, cnt.linkurl AS linkurl'
        . ' FROM `#__vm_competition_winners` AS w'
        . ' INNER JOIN `#__vm_competition` AS cmp ON cmp.id = w.`comp_id`'
        . ' LEFT JOIN `#__vm_competition_contestants` AS cnt ON cnt.cid = w.cont_id'
        . ' ORDER BY w.wid DESC'
        ;
        
        /*
        $query = 'SELECT ct . * , cmp.title AS comp_name'
        . ' FROM `#__vm_competition_contestants` AS ct'
        . ' INNER JOIN `#__vm_competition` AS cmp ON cmp.id = ct.`comp_id`'
        ;
        */
        return $query;
    }
 
    /**
     * Retrieves the hello data
     * @return array Array of objects containing the data from the database
     */
    function getData()
    {
        // Lets load the data if it doesn't already exist
        if (empty( $this->_data ))
        {
            $query = $this->_buildQuery();
             $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit')); 
        }
 
        return $this->_data;
    }
    
    /**
     * Get total number of rows
     */
	function getTotal()
	{
        // Load the content if it doesn't already exist
        if (empty($this->_total)) {
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);    
        }
        return $this->_total;
	}
	
	/**
	 * Get pagination object
	 */
    function getPagination()
  {
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
  }
  
    
}



?>